//
//  VRViewBackVideoViewController.m
//  VRFirefighting
//
//  Created by Veer on 2018/8/25.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "VRViewBackVideoViewController.h"

#import <EZOpenSDKFramework/EZOpenSDK.h>
#import <EZOpenSDKFramework/EZPlayer.h>
#import "EZUIKit.h"
#import "EZUIPlayer.h"
#import "EZUIError.h"
#import "Toast+UIView.h"
#import "EZPlaybackProgressBar.h"
#import <EZOpenSDKFramework/EZDeviceRecordFile.h>
#import <EZOpenSDKFramework/EZCloudRecordFile.h>
//#import "EZDeviceRecordFile.h"
//#import "EZCloudRecordFile.h"
#import "DDCollectionViewFlowLayout.h"
#import "EZRecordCell.h"
#import "VRBackVideoCell.h"
#import "DDKit.h"

#import "UIImage+VR.h"
#import "HIKLoadView.h"

@interface VRViewBackVideoViewController () <EZUIPlayerDelegate, EZPlayerDelegate, DDCollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate>
{
    BOOL _isPlaying;
    NSTimeInterval _playSeconds; //播放秒数
    NSTimeInterval _duringSeconds; //录像时长
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UICollectionView *playbackList;
@property (nonatomic, strong) UIView *listBgView;
@property (nonatomic, strong) UIView *nodataView;
@property (nonatomic, strong) UILabel *nodataTitleLbl;
/** 播放器 */
@property (nonatomic, strong) UIView *playerView;
@property (nonatomic, strong) UIView *playBoxView;
@property (nonatomic, strong) UILabel *playTimeLabel;
@property (nonatomic, strong) UILabel *duringTimeLabel;
@property (nonatomic, strong) UISlider *duringSlider;
@property (nonatomic, strong) HIKLoadView *loadingView;


@property (nonatomic, strong) EZPlayer *player;
@property (nonatomic, copy) NSString *appKey;
@property (nonatomic, copy) NSString *token;
@property (nonatomic, copy) NSString *channelno;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, copy) NSString *deviceserial;

@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger currentSelectType;

@property (nonatomic, strong) NSMutableArray *records;
@property (nonatomic, strong) EZDeviceRecordFile *deviceRecord;
@property (nonatomic, strong) EZCloudRecordFile *cloudRecord;
@property (nonatomic, strong) NSDate *beginTime;
@property (nonatomic, strong) NSDate *endTime;

@property (nonatomic, strong) NSTimer *playbackTimer;

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@end

@implementation VRViewBackVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy年MM月dd日";
    self.title = [dateFormatter stringFromDate:[NSDate date]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.records = [NSMutableArray array];
    
    [self setupSubView];
    [self getDeviceDataWithType:self.fID];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    if (self.playbackTimer)
    {
        [self.playbackTimer invalidate];
        self.playbackTimer = nil;
    }
    [self.loadingView stopSquareClockwiseAnimation];
    [_player closeSound];
    [_player stopPlayback];
    [super viewWillDisappear:animated];
}

- (void)dealloc
{
    [self releasePlayer];
}

- (void)setupSubView
{
    
    UIView *listBgView = [[UIView alloc] init];
    listBgView.backgroundColor = RGB(255, 255, 255);
    listBgView.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, kScreenWidth, 64);
    [self.view addSubview:listBgView];
    self.listBgView = listBgView;
    
    UIView *listView = [[UIView alloc] init];
    listView.layer.borderColor = MAINColor.CGColor;
    listView.layer.borderWidth = 1.0;
    listView.frame = CGRectMake(0, 0, kScreenWidth - 20 * 2, 40);
    listView.center = CGPointMake(kScreenWidth * 0.5, 32);
    [listBgView addSubview:listView];
    
    UILabel *detailLbl = [[UILabel alloc] init];
    detailLbl.text = self.ModelName;
    detailLbl.textColor = MAINColor;
    detailLbl.font = VRFont(14);
    detailLbl.textAlignment = NSTextAlignmentCenter;
    detailLbl.frame = listView.bounds;
    [listView addSubview:detailLbl];

    UIButton *leftBtn = [XButton buttonWithType:UIButtonTypeCustom];
    leftBtn.adjustsImageWhenHighlighted = NO;
    leftBtn.enabled = NO;
    leftBtn.tag = 0;
    [leftBtn setTitle:@"SD卡录像" forState:UIControlStateNormal];
    leftBtn.titleLabel.font = VRFont(16);
    [leftBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [leftBtn setTitleColor:RGB(78, 172, 57) forState:UIControlStateDisabled];
    [leftBtn setBackgroundImage:[UIImage imageWithColor:RGB(200, 200, 200)] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateDisabled];
    [leftBtn addTarget:self action:@selector(onSegmentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    self.leftBtn = leftBtn;
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.playerView.mas_bottom).offset(10);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.adjustsImageWhenHighlighted = NO;
    rightBtn.tag = 1;
    [rightBtn setTitle:@"云录像" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = VRFont(16);
    [rightBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [rightBtn setTitleColor:RGB(78, 172, 57) forState:UIControlStateDisabled];
    [rightBtn setBackgroundImage:[UIImage imageWithColor:RGB(200, 200, 200)] forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateDisabled];
    [rightBtn addTarget:self action:@selector(onSegmentBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightBtn];
    self.rightBtn = rightBtn;
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(leftBtn);
        make.left.mas_equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(40);
    }];
    
//    DDCollectionViewFlowLayout *flowLayout = [[DDCollectionViewFlowLayout alloc] init];
//    flowLayout.delegate = self;
//
//    self.playbackList = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.playerView.frame), kScreenWidth, 200) collectionViewLayout:flowLayout];
//    self.playbackList.backgroundColor = ScrollViewBgColor;
//    [self.playbackList registerClass:[EZRecordCell class] forCellWithReuseIdentifier:@"RecordCell"];
////    self.playbackList = [[UICollectionView alloc] initwi];
//    self.playbackList.dataSource = self;
//    self.playbackList.delegate = self;
//    [self.playbackList setCollectionViewLayout:flowLayout];
//    [self.view addSubview:self.playbackList];
//    [self.playbackList mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.mas_equalTo(self.view);
//        make.top.mas_equalTo(self.playerView.mas_bottom).offset(50);
//    }];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, kScreenHeight - NAVIGATION_BAR_HEIGHT - 40) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = [[UIView alloc] init];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    _tableView.tableFooterView = [[UIView alloc] init];;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.playerView.mas_bottom).offset(50);
    }];

    self.nodataView.y = CGRectGetMaxY(self.playerView.frame) + 50 + 40;
    
    
    if(!_loadingView)
        _loadingView = [[HIKLoadView alloc] initWithHIKLoadViewStyle:HIKLoadViewStyleSqureClockWise];
    //    [self.view insertSubview:_loadingView aboveSubview:self.playerView];
    [self.view addSubview:_loadingView];
    [_loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(20);
        make.centerX.mas_equalTo(self.playerView.mas_centerX);
        make.centerY.mas_equalTo(self.playerView.mas_centerY);
    }];
    [_loadingView stopSquareClockwiseAnimation];
}


- (void)setupPlayer
{
    
    if (self.player == nil) {
        
        self.player = [EZOpenSDK createPlayerWithDeviceSerial:self.deviceserial cameraNo:[self.channelno integerValue]];
        self.player.delegate = self;
        [self.player setPlayerView:self.playerView];
    }
    
    [self searchRecord];
}

- (void) releasePlayer
{
    if (self.player)
    {
        [EZOpenSDK releasePlayer:self.player];
        self.player = nil;
    }
}

- (void)searchRecord
{
    if (self.currentSelectType == 0) {
        
        weakify(self)
        [EZOpenSDK searchRecordFileFromDevice:self.deviceserial cameraNo:[self.channelno integerValue] beginTime:self.beginTime endTime:self.endTime completion:^(NSArray *deviceRecords, NSError *error) {
            
            strongify(self)
            [self.records addObjectsFromArray:deviceRecords];
            self.deviceRecord = [self.records firstObject];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (deviceRecords.count > 0) {
                    [self.loadingView startSquareClcokwiseAnimation];
                }
                self.nodataView.hidden = self.records.count > 0;
                [self duringTimeShow];
                [self.player startPlaybackFromDevice:self.deviceRecord];
                [self.playbackList reloadData];
                [self.tableView reloadData];
            });
            
        }];
    }else{
        
        weakify(self)
        [EZOpenSDK searchRecordFileFromCloud:self.deviceserial cameraNo:[self.channelno integerValue] beginTime:self.beginTime endTime:self.endTime completion:^(NSArray *couldRecords, NSError *error) {
            
            strongify(self)
            if (couldRecords.count > 0) {
                [self.loadingView startSquareClcokwiseAnimation];
            }
            self.cloudRecord = [self.records firstObject];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.loadingView startSquareClcokwiseAnimation];
                self.nodataView.hidden = self.records.count > 0;
                [self duringTimeShow];
                [self.player startPlaybackFromCloud:self.cloudRecord];
                [self.playbackList reloadData];
                [self.tableView reloadData];
            });
        }];
    }
    
    
    
}


- (void)duringTimeShow
{
    if(self.currentSelectType == 0)
    {
        _duringSeconds = [_deviceRecord.stopTime timeIntervalSinceDate:_deviceRecord.startTime];
    }
    else
    {
        _duringSeconds = [_cloudRecord.stopTime timeIntervalSinceDate:_cloudRecord.startTime];
    }
    
    if(_duringSeconds >= 3600)
    {
        int hour = (int)_duringSeconds / 3600;
        int minute =  ((int)_duringSeconds % 3600) / 60;
        int seconds = (int)_duringSeconds % 60;
        self.duringTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, seconds];
        self.playTimeLabel.text = @"00:00:00";
    }
    else
    {
        int minute =  (int)_duringSeconds / 60;
        int seconds = (int)_duringSeconds % 60;
        self.duringTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d", minute, seconds];
        self.playTimeLabel.text = @"00:00";
    }
}


#pragma mark - networking
- (void)getDeviceDataWithType:(NSString *)type
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //    param[@"UserID"] = [VRUser shareUser].token;
    //    param[@"fIsAreaManage"] = @([VRUser shareUser].fIsAreaManage);
    param[@"fID"] = type;
    
    weakify(self)
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    hud.bezelView.backgroundColor = RGBA(0, 0, 0, 0.9);
    //    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    hud.mode = MBProgressHUDModeIndeterminate;
    [JKNetworking postWithSign:GetMonitorBackVideo parameters:param success:^(NSDictionary *responseObject) {
        
        strongify(self)
        NSLog(@"responseObject:%@", responseObject);
        NSInteger code = [responseObject[@"state"] integerValue];
        NSString *msg = responseObject[@"message"];
        
        if (code == 0) {
            
            self.appKey = responseObject[@"data"][@"appKey"];
            self.channelno = responseObject[@"data"][@"channelno"];
            self.code = responseObject[@"data"][@"code"];
            self.deviceserial = responseObject[@"data"][@"deviceserial"];
            self.token = responseObject[@"data"][@"token"];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self setupPlayer];
                //                [self.tableView reloadData];
                [hud hideAnimated:YES];
            });
        }else if(msg.length > 0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:msg];
                [hud hideAnimated:YES];
            });
        }
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"未知错误"];
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
        });
    }];
}


#pragma mark - action
- (void)onSegmentBtnClick:(UIButton *)sender
{
    self.nodataView.hidden = YES;
    self.nodataTitleLbl.text = sender.tag == 0 ? @"SD卡没有回放录像！": @"云存储没有回放录像！";
    self.selectedIndexPath = nil;
    self.currentSelectType = sender.tag;
    self.leftBtn.enabled = !self.leftBtn.enabled;
    self.rightBtn.enabled = !self.rightBtn.enabled;
    
    [self.records removeAllObjects];
    [self.playbackList reloadData];
    [self.tableView reloadData];
    [self searchRecord];
}


- (void)playBoxToolRefresh:(NSTimer *)timer
{
    NSDate *currentTime = [_player getOSDTime];
    if(self.currentSelectType == 0)
    {
        _playSeconds = [currentTime timeIntervalSinceDate:_deviceRecord.startTime];
    }
    else
    {
        _playSeconds = [currentTime timeIntervalSinceDate:_cloudRecord.startTime];
    }
    
    if(_playSeconds >= 3600)
    {
        int hour = (int)_playSeconds / 3600;
        int minute =  ((int)_playSeconds % 3600) / 60;
        int seconds = (int)_playSeconds % 60;
        self.playTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, seconds];
    }
    else
    {
        int minute =  (int)_playSeconds / 60;
        int seconds = (int)_playSeconds % 60;
        self.playTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d", minute, seconds];
    }
    
    self.duringSlider.value = _playSeconds/_duringSeconds;
}


- (void)duringValueChange:(id)sender
{
    NSDate *offsetTime = nil;
    if(self.currentSelectType == 0)
    {
        offsetTime = [_deviceRecord.startTime dateByAddingTimeInterval:_duringSeconds * self.duringSlider.value];
    }
    else
    {
        offsetTime = [_cloudRecord.startTime dateByAddingTimeInterval:_duringSeconds * self.duringSlider.value];
    }
    if (self.playbackTimer)
    {
        [self.playbackTimer invalidate];
        self.playbackTimer = nil;
    }
    [_player seekPlayback:offsetTime];
    [self.loadingView startSquareClcokwiseAnimation];
}


//#pragma mark - UICollectionViewDataSource Methods
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    return [_records count];
//}
//
//- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(DDCollectionViewFlowLayout *)layout numberOfColumnsInSection:(NSInteger)section
//{
//    return 3;
//}
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    EZRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecordCell" forIndexPath:indexPath];
//    cell.deviceSerial = self.deviceserial;
//    if(self.currentSelectType == 0)
//    {
//        [cell setDeviceRecord:[_records dd_objectAtIndex:indexPath.row] selected:(indexPath.row == self.selectedIndexPath.row)];
//    }
//    else
//    {
//        [cell setCloudRecord:[_records dd_objectAtIndex:indexPath.row] selected:(indexPath.row == self.selectedIndexPath.row)];
//    }
//    return cell;
//}
//
//#pragma mark - UICollectionViewDelegate Methods
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(106 * [UIScreen mainScreen].bounds.size.width / 320.0f, 80.0 * [UIScreen mainScreen].bounds.size.width / 320.0f);
//}
//
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    if(self.currentSelectType == 0)
//    {
//        _deviceRecord = [_records dd_objectAtIndex:indexPath.row];
//        [_player startPlaybackFromDevice:_deviceRecord];
//    }
//    else
//    {
//        _cloudRecord = [_records dd_objectAtIndex:_records.count - 1 - indexPath.row];
//        [_player startPlaybackFromCloud:_cloudRecord];
//    }
//
//    [self duringTimeShow];
//
//    self.selectedIndexPath = indexPath;
//    [collectionView reloadData];
//
////    [self.loadingView startSquareClcokwiseAnimation];
//
//}


#pragma mark - tableViewdelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_records count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VRBackVideoCell *cell = [VRBackVideoCell cellWith:tableView];
    if(self.currentSelectType == 0)
    {
        [cell setDeviceRecord:[_records dd_objectAtIndex:indexPath.row] selected:(indexPath.row == self.selectedIndexPath.row)];
    }
    else
    {
        [cell setCloudRecord:[_records dd_objectAtIndex:indexPath.row] selected:(indexPath.row == self.selectedIndexPath.row)];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.currentSelectType == 0)
    {
        _deviceRecord = [_records dd_objectAtIndex:indexPath.row];
        [_player startPlaybackFromDevice:_deviceRecord];
    }
    else
    {
        _cloudRecord = [_records dd_objectAtIndex:_records.count - 1 - indexPath.row];
        [_player startPlaybackFromCloud:_cloudRecord];
    }
    
    [self duringTimeShow];
    
    self.selectedIndexPath = indexPath;
    [tableView reloadData];
    [self.loadingView startSquareClcokwiseAnimation];
}



#pragma mark - PlayerDelegate Methods
- (void)player:(EZPlayer *)player didPlayFailed:(NSError *)error
{
    NSLog(@"player: %@ didPlayFailed: %@", player, error);
    //如果是需要验证码或者是验证码错误
    if (error.code == EZ_SDK_NEED_VALIDATECODE) {
//        [self showSetPassword];
        return;
    } else if (error.code == EZ_SDK_VALIDATECODE_NOT_MATCH) {
//        [self showRetry];
        return;
    }
    else if (error.code == 34)//特殊处理
    {
        [_player stopPlayback];
        if (self.currentSelectType == 0)
        {
            _deviceRecord = [_records firstObject];
            [_player startPlaybackFromDevice:_deviceRecord];
        }
        else
        {
            _cloudRecord = [_records lastObject];
            [_player startPlaybackFromCloud:_cloudRecord];
        }
        return;
        
    }
    [UIView dd_showDetailMessage:[NSString stringWithFormat:@"%d", (int)error.code]];
    if (error.code == 400409) {
        _isPlaying = NO;
//        [self.playButton setImage:[UIImage imageNamed:@"preview_play_btn_sel"] forState:UIControlStateHighlighted];
//        [self.playButton setImage:[UIImage imageNamed:@"preview_play_btn"] forState:UIControlStateNormal];
    }
}

- (void)player:(EZPlayer *)player didReceivedMessage:(NSInteger)messageCode
{
    NSLog(@"player: %@ didReceivedMessage: %d", player, (int)messageCode);
    if(messageCode == PLAYER_PLAYBACK_START)
    {
        _isPlaying = YES;
//        [self.playButton setImage:[UIImage imageNamed:@"pause_sel"] forState:UIControlStateHighlighted];
//        [self.playButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
        [self.loadingView stopSquareClockwiseAnimation];
        
//        if (!_isOpenSound)
//        {
//            [player closeSound];
//        }
        
        if (self.playbackTimer)
        {
            [self.playbackTimer invalidate];
            self.playbackTimer = nil;
        }
        
        self.playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                              target:self
                                                            selector:@selector(playBoxToolRefresh:)
                                                            userInfo:nil
                                                             repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.playbackTimer forMode:NSRunLoopCommonModes];
        
//        [self performSelector:@selector(hiddenPlayerToolbox:) withObject:nil afterDelay:5.0f];
        
    }
    else if (messageCode == PLAYER_PLAYBACK_STOP)
    {
        if(self.playbackTimer)
        {
            [self.playbackTimer invalidate];
            self.playbackTimer = nil;
        }
    }
}





//- (EZUIPlayer *)mPlayer
//{
//    if (_mPlayer == nil) {
//
//        _mPlayer = [[EZUIPlayer alloc] init];
//        _mPlayer.mDelegate = self;
//        //添加预览视图到当前界面
//        _mPlayer.previewView.frame = CGRectMake(20, NAVIGATION_BAR_HEIGHT + 64 +20, kScreenWidth - 40, CGRectGetHeight(_mPlayer.previewView.frame));
//        _mPlayer.customIndicatorView.centerX = (kScreenWidth - 40) * 0.5;
//        //        [self.view insertSubview:_mPlayer.previewView belowSubview:self.listBgView];
//        [self.view addSubview:_mPlayer.previewView];
//    }
//    return _mPlayer;
//}
     
     
#pragma mark - Get Methods
- (NSDate *)beginTime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *beginTimeString = [NSString stringWithFormat:@"%@ 00:00:00", [dateFormatter stringFromDate:[NSDate date]]];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    _beginTime = [dateFormatter dateFromString:beginTimeString];
    return _beginTime;
}
 
 - (NSDate *)endTime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *beginTimeString = [NSString stringWithFormat:@"%@ 23:59:59", [dateFormatter stringFromDate:[NSDate date]]];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    _endTime = [dateFormatter dateFromString:beginTimeString];
    return _endTime;
}

- (UIView *)playerView
{
    if (_playerView == nil) {
        
        _playerView = [[UIView alloc] init];
        _playerView.backgroundColor = [UIColor blackColor];
        _playerView.frame = CGRectMake(20, NAVIGATION_BAR_HEIGHT + 64, kScreenWidth - 40, 200);
        [self.view addSubview:_playerView];
        
        UIView *playBoxView = [[UIView alloc] init];
        playBoxView.backgroundColor = RGBA(0, 0, 0, 0.5);
        [self.view addSubview:playBoxView];
        playBoxView.frame = CGRectMake(20, CGRectGetMaxY(_playerView.frame) - 30, kScreenWidth - 40, 30);
//        [playBoxView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(0);
//            make.bottom.mas_equalTo(self->_playerView.mas_bottom);
//            make.right.mas_equalTo(0);
//            make.height.mas_equalTo(30);
//        }];
        
        UILabel *playTimeLabel = [[UILabel alloc] init];
        playTimeLabel.text = @"00:00:00";
        playTimeLabel.textColor = RGB(255, 255, 255);
        playTimeLabel.font = VRFont(12);
        playTimeLabel.textAlignment = NSTextAlignmentRight;
        [playBoxView addSubview:playTimeLabel];
        self.playTimeLabel = playTimeLabel;
        [playTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(4);
            make.centerY.mas_equalTo(playBoxView);
            make.height.mas_equalTo(20);
        }];
        
        UILabel *duringTimeLabel = [[UILabel alloc] init];
        duringTimeLabel.text = @"00:00:00";
        duringTimeLabel.textColor = RGB(255, 255, 255);
        duringTimeLabel.font = VRFont(12);
        duringTimeLabel.textAlignment = NSTextAlignmentRight;
        [playBoxView addSubview:duringTimeLabel];
        self.duringTimeLabel = duringTimeLabel;
        [duringTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-4);
            make.centerY.mas_equalTo(playBoxView);
            make.height.mas_equalTo(20);
        }];
        
        UISlider *slider = [[UISlider alloc] init];
        slider.thumbTintColor = RGB(21, 126, 251);
        slider.tintColor = RGB(21, 126, 251);
        [slider setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
        [slider setThumbImage:[UIImage imageNamed:@"slider_sel"] forState:UIControlStateHighlighted];
        [slider addTarget:self action:@selector(duringValueChange:) forControlEvents:UIControlEventValueChanged];
        [playBoxView addSubview:slider];
        self.duringSlider = slider;
        [slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(playTimeLabel.mas_right).offset(4);
            make.right.mas_equalTo(duringTimeLabel.mas_left).offset(-4);
            make.centerY.mas_equalTo(playBoxView);
            make.height.mas_equalTo(20);
        }];
    }
    return _playerView;
}

- (UIView *)nodataView
{
    if (_nodataView == nil) {
        
        _nodataView = [[UIView alloc] init];
        _nodataView.hidden = YES;
        _nodataView.frame = CGRectMake(0, 0, 200, 200);
        _nodataView.centerX = kScreenWidth * 0.5;
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"no_video"];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.frame = CGRectMake(0, 0, _nodataView.width, 100);
        [_nodataView addSubview:imageView];
        
        UILabel *title = [[UILabel alloc] init];
        title.text = @"SD卡没有回放录像！";
        title.textColor = RGB(140, 140, 140);
        title.font = VRFont(18);
        title.textAlignment = NSTextAlignmentCenter;
        title.frame = CGRectMake(0, imageView.height + 20, _nodataView.width, 20);
        [_nodataView addSubview:title];
        self.nodataTitleLbl = title;
        
        [self.view addSubview:_nodataView];
    }
    return _nodataView;
}


@end
